package scu.debate.web.utils;

import scu.debate.dao.entity.Contest;
import scu.debate.dao.entity.User;

import java.util.Map;

public class InterUtil {
    public static Contest convertToContest(Map<String, Object> map) {
        Integer contestId = (Integer) map.get("contestid");
        String contestName = (String) map.get("contestname");
        String proponent = (String) map.get("proponent");
        String opponent = (String) map.get("opponent");
        Double pscore = Double.parseDouble((String) map.get("pscore"));
        Double oscore = Double.parseDouble((String) map.get("oscore"));
        String link = (String) map.get("link");
        Integer argumentId = (Integer) map.get("argumentId");
        Contest contest = new Contest();
        contest.setContestid(contestId);
        contest.setContestname(contestName);
        contest.setOpponent(opponent);
        contest.setArgumentId(argumentId);
        contest.setProponent(proponent);
        contest.setPscore(pscore);
        contest.setOscore(oscore);
        contest.setLink(link);
        return contest;
    }

    public static User convertToUser(Map<String,Object> map){
        User user = new User();
        String userName = (String) map.get("userName");
        String password = (String) map.get("password");
        String mailAddress = (String) map.get("mailAddress");
        Integer level = (Integer) map.get("level");
        String trueName = (String) map.get("trueName");
        String schoolName = (String) map.get("schoolName");
        String collegeName = (String) map.get("collegeName");
        Integer grade = (Integer) map.get("grade");
        String gender = (String) map.get("gender");
        String account = (String) map.get("account");
        if (account == null){
            account = mailAddress;
        }
        String truePassword = account + password.hashCode();
        user.setUserName(userName);
        user.setPassword(truePassword);
        user.setMailAddress(mailAddress);
        user.setLevel(level);
        user.setTrueName(trueName);
        user.setSchoolName(schoolName);
        user.setCollegeName(collegeName);
        user.setGrade(grade);
        user.setGender(gender);
        user.setAccount(account);
        return user;
    }
}
